package com.tools.photo.ui.view;


import android.content.Context;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.tools.photo.R;
import com.tools.photo.bean.PhotoUpImageBucket;
import com.tools.photo.bean.PhotoUpImageItem;
import com.tools.photo.ui.widget.CKBottomSheetDialog;
import com.yqritc.recyclerviewflexibledivider.HorizontalDividerItemDecoration;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class BottomSheetDialogView {


    View.OnClickListener onClickListener;

    CKBottomSheetDialog dialog;

    int selectIndex;

    /**
     * remember to call setLocalNightMode for dialog
     *
     * @param context
     * @param dayNightMode current day night mode
     */
    public BottomSheetDialogView(Context context, int dayNightMode,List<PhotoUpImageBucket> mItems,int total,PhotoUpImageItem model,int selectindex) {
        selectIndex=selectindex;
        dialog = new CKBottomSheetDialog(context);
        dialog.getDelegate().setLocalNightMode(dayNightMode);
        View view = LayoutInflater.from(context).inflate(R.layout.bottom_sheet_dialog_recycler_view, null);

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.bottom_sheet_recycler_view);
        LinearLayoutManager layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.addItemDecoration(new HorizontalDividerItemDecoration.Builder(context).build());
        List<PhotoUpImageBucket> temp= new ArrayList<>();
        PhotoUpImageBucket photoUpImageBucket=new PhotoUpImageBucket();
        photoUpImageBucket.setBucketName("所有图片");
        photoUpImageBucket.setCount(total);
        photoUpImageBucket.setBucketPath(model.getImagePath());
        temp.add(photoUpImageBucket);
        temp.addAll(mItems);
        recyclerView.setAdapter(new SimpleAdapter(temp));
        dialog.setContentView(view);
        dialog.show();
    }

    public void setOnItemsClickListener(View.OnClickListener onItemsClickListener){
        this.onClickListener=onItemsClickListener;
    }

     class ViewHolder extends RecyclerView.ViewHolder {

        private TextView mTextView;
         private ImageView img;
         private RadioButton radioButton;

        public ViewHolder(View itemView) {
            super(itemView);
            mTextView = (TextView) itemView.findViewById(R.id.list_item_text_view);
            img=(ImageView)itemView.findViewById(R.id.img);
            radioButton=(RadioButton)itemView.findViewById(R.id.radio);
        }
    }

    public class SimpleAdapter extends RecyclerView.Adapter<ViewHolder> {


        List<PhotoUpImageBucket> mItems;
        public SimpleAdapter(List<PhotoUpImageBucket> mItems) {
            this.mItems=mItems;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            View view = inflater.inflate(R.layout.list_item, parent,false);
            return new ViewHolder(view);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, final int position) {
            PhotoUpImageBucket model=mItems.get(position);
            holder.mTextView.setText(model.getBucketName()+"("+model.getCount()+")");
            Glide.with(holder.itemView.getContext()).load(model.getBucketPath()).into(holder.img);
            if (selectIndex==position){
                holder.radioButton.setVisibility(View.VISIBLE);
            }else {
                holder.radioButton.setVisibility(View.GONE);
            }
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dialog.dismiss();
                    v.setId(position);
                    onClickListener.onClick(v);
                }
            });
        }

        @Override
        public int getItemCount() {
            return mItems.size();
        }
    }
}
